#ifndef TCP485CONNECT_H
#define TCP485CONNECT_H


#include <QTcpSocket>
#include <QDebug>
#include <QObject>
#include <QHostAddress>
//#include <QAbstractSocket>
#include <QFuture>
#include <QTimer>
#include "machineparam.h"


class TcpClient : public QTcpSocket {
    Q_OBJECT
public:
    TcpClient();
    ~TcpClient();
    void connectToServer(const QString &host, quint16 port) {
        connectToHost(host, port);

        //设置超时时间

        timer->start(2000);//

        connect(timer, &QTimer::timeout, [=]() {
            qDebug() <<"connet timeout:" << host << " port:" << port;
            disconnectFromHost(); // 断开连接
            emit error(QAbstractSocket::SocketTimeoutError); // 发出超时信号
            timer->stop();
            timer->disconnect();
        });
    }

    MachineReadParam *getReadParam();
    MachineWirteParam *getWriteParam();
    bool execWriteParam();

     QFuture<QByteArray> sendData(const QByteArray &data);
    QByteArray syncSendData(const QByteArray &data);

private slots:
    void onConnected();
    void onReadyRead() {
        QByteArray data = readAll();
        qDebug() << "Received data:" << data;
    }

    void onError(QAbstractSocket::SocketError socketError);

signals:
    void error(QAbstractSocket::SocketError);
    void connected();
    void emitReadParam(MachineReadParam param);

private:
    MachineReadParam *m_readParam;
    MachineWirteParam *m_writeParam;

    QTimer *timer;
};
#endif // TCP485CONNECT_H
